package com.big.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.big.app.config.RedisConfig;
import com.big.app.dao.SightDao;
import com.big.app.dto.SightCommentDto;
import com.big.app.dto.SightDto;
import com.big.app.entity.Sight;
import com.big.app.service.intf.SightService;
import com.big.app.util.JedisUtils;
import com.big.app.vo1.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * .::::.
 * .::::::::.
 * :::::::::::  I && YOU
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ````':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 */
@Service
public class SightServiceImpl implements SightService {
    //实例化日志对象
    private Logger logger= LoggerFactory.getLogger(SightServiceImpl.class);
    @Resource
    private SightDao dao;
    /*
    * 1.景点主页查询显示
    * */
    @Override
    public R queryHotTop() {
        logger.info("热门景点访问开始");
        List<SightDto> list;
        //1.判断有缓存没
        if (JedisUtils.esists(RedisConfig.HOT_SIGHT)){
            logger.info("访问Redis");
            //2.将缓存数据字符串转换为json数组格式并传给集合.
            list=JSON.parseArray(JedisUtils.getStr(RedisConfig.HOT_SIGHT),SightDto.class);
            logger.info("Redis访问结束");
        }else {
            //没有缓存
            logger.info("访问数据库");
            list=dao.queryHotTop();
            if(list==null){
                return R.fail("暂无热门景点");
            }
            logger.info("数据库访问结束");
        }
        logger.info("访问结束");
        return R.ok(list);
    }
    /*
    * 2.根据景区名字搜索
    * */
    public R search(String title){
        if (title!=null){
            title="%"+title+"%";
            List<SightDto> list=dao.search(title);
            if (list!=null){
                return R.ok(list);
            }else {
                return R.fail("没有查到相关内容!");
            }
        }
        return R.fail("搜索不能为空");
    }
    /*
     * 3.景区详情
     * */
    public R detail (int id){
        if (id!=0){
            Sight sight = dao.selectById(id);
            if (sight!=null){
                return R.ok(sight);
            }else {
                return R.fail("网络丢失了~");
            }
        }
        return R.fail("查看异常");
    }

    @Override
    public R comment(int id) {
        if (id!=0) {
            List<SightCommentDto> list = dao.queryComment(id);
            if (list != null) {
                return R.ok(list);
            } else {
                return R.fail("暂无评论！");
            }
        }
        return R.fail("查看出错了！");
    }
}
